//
//  XDPlaceCityViewController.m
//  Le way walkers
//
//  Created by YaZhiLiang on 15/5/23.
//  Copyright (c) 2015年 LTXZ. All rights reserved.
//

#import "XDPlaceCityViewController.h"

@interface XDPlaceCityViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) UIImageView *topView;

@property (nonatomic, retain) UIImageView *gifImageView;

@property (nonatomic, retain) UIView *gifView;

@end

@implementation XDPlaceCityViewController


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        
    }
    
    return self;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.collectionView.delegate = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.collectionView.delegate = self;
}

- (void)getData
{
    
    NSLog(@"------------------ %@", self.typeName);
    
    NSLog(@"--------!!!!--------- %@", self.ScenicID);
    
    [NNetworkHandle getDataWithUrl:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/", self.typeName, self.ScenicID] Cookie:@"bt_devid=i_70977b3f76167894392f3ada99cb3f82fb57705ae698d0fc08ca99fd7f26cdf9057e84df; Hm_lvt_39ccc5353de4e1969b414e80e516d5a7=1431606395,1431674200,1431763985,1431910698; sessionid=75f121c0f7165c1e3eef07a24359f126" completion:^(id result) {
        
        self.imageUrl = [[[result objectForKey:@"hottest_places"] objectAtIndex:0] objectForKey:@"photo"];
        
        self.Arr = [NSMutableArray array];
        
        self.xdPlaceCity = [[XDPlaceCityView alloc] initWithDic:result];
        
        [self.Arr addObject:self.xdPlaceCity];
        
        [_xdPlaceCity release];
        NSLog(@"-----------%@", self.xdPlaceCity.name);
        
        NSLog(@"--------- result ----%@", result);
        
        [self.topView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImage imageNamed:@"69E58PIChJC_1024.jpg"]];
        
        if ([result objectForKey:@"recommended_reason"] == [NSNull null]) {
            
            
        }
        else
        {
         
         self.recommended_reason = [result objectForKey:@"recommended_reason"];
            
        }
        
        if ([result objectForKey:@"description"] == [NSNull null]) {
            
            
            
        }
        else
        {
            
         self.description_details = [result objectForKey:@"description"];

            
        }
        
        
        self.address = [result objectForKey:@"address"];
        
        self.name = [result objectForKey:@"name"];
        
        [self.collectionView reloadData];
        
        [self.gifImageView stopAnimating];
        
        self.gifView.hidden = YES;
        
    }];
    
}

- (void)yangTuo
{
    [self getData];
    self.gifImageView.hidden = NO;
    self.gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 40, self.view.frame.size.height / 2 - 50, 80, 100)];
    NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"cao1.png"],
                         [UIImage imageNamed:@"cao2.png"],
                         [UIImage imageNamed:@"cao3.png"],
                         [UIImage imageNamed:@"cao4.png"],
                         [UIImage imageNamed:@"cao5.png"],
                         [UIImage imageNamed:@"cao6.png"],
                         [UIImage imageNamed:@"cao7.png"],
                         [UIImage imageNamed:@"cao8.png"],
                         [UIImage imageNamed:@"cao9.png"],
                         [UIImage imageNamed:@"cao10.png"],nil];
    self.gifImageView.animationImages = gifArray; //动画图片数组
    self.gifImageView.animationDuration = 0.8; //执行一次完整动画所需的时长
    self.gifImageView.animationRepeatCount = 10000;  //动画重复次数
    self.gifView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    UIColor *imageB = [UIColor colorWithPatternImage:[UIImage imageNamed:@"123456_123456"]];
    self.gifView.backgroundColor = imageB;
    [self.gifView addSubview:self.gifImageView];
    [self.gifImageView startAnimating];
    [self.view addSubview:self.gifView];
    [_gifImageView release];
    [_gifView release];
}

- (void)createSubTableView
{
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"iconfont-xiangzuo"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowlayout];
    
//    self.collectionView.backgroundColor = [UIColor whiteColor];
    
        self.collectionView.alwaysBounceVertical = YES;
    
    UIColor *image1 = [UIColor colorWithPatternImage:[UIImage imageNamed:@"123456_123456"]];
    
    self.collectionView.backgroundColor = image1;
    
    flowlayout.itemSize = CGSizeMake(self.view.frame.size.width - 40, self.view.frame.size.height);
    
    flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    
    flowlayout.sectionInset = UIEdgeInsetsMake(50, 20, 0, 20);
    
    self.collectionView.delegate = self;
    
    self.collectionView.dataSource = self;
    
    self.collectionView.alwaysBounceVertical = YES;
    
    [self.view addSubview:self.collectionView];
    
    [_collectionView release];
    
    [flowlayout release];
    
    self.collectionView.contentInset = UIEdgeInsetsMake(kImageOriginHight, 0, 280, 0);
    
    self.topView = [[UIImageView alloc]init];
    
    self.topView.frame = CGRectMake(0, -kImageOriginHight - kTempHeight, self.collectionView.frame.size.width, kImageOriginHight + kTempHeight);
    
    self.topView.userInteractionEnabled = YES;
    
//    self.topView.backgroundColor = [UIColor orangeColor];
    
    [self.collectionView addSubview:self.topView];
    
    [_topView release];
    
//    UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeCustom];
//    
//    [mybutton setImage:[[UIImage imageNamed:@"iconfont-xiangzuo"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
//    
//    mybutton.frame = CGRectMake(self.collectionView.frame.origin.x + 10, 64 + 20, 50, 30);
//    
//    [mybutton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    
    //    mybutton.backgroundColor = [UIColor blackColor];
    
//    [self.topView addSubview:mybutton];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    
    [self.topView addGestureRecognizer:tap];
    
    [tap release];
    
    [self.collectionView registerClass:[XDCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}

- (void)back:(UIBarButtonItem *)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    XDPlaceDetailsRegionViewController *xdplaceDetailsVC = [[[XDPlaceDetailsRegionViewController alloc]init] autorelease];
    
    xdplaceDetailsVC.ScenicID = self.ScenicID;
    
    xdplaceDetailsVC.typeName = self.typeName;
    
    xdplaceDetailsVC.titleName = self.name;
    
    NSLog(@"---------- ****** %@", xdplaceDetailsVC.titleName);
    
    [self.navigationController pushViewController:xdplaceDetailsVC animated:YES];
    
}

static CGFloat kImageOriginHight = 250;
static CGFloat kTempHeight = 60.0f;

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    static NSInteger i = 0;
//    if ((i = (scrollView.contentOffset.y + 60) / 3)) {
//        self.navigationController.navigationBar.alpha = 0.1 * i ;
//    }
    CGFloat yOffset  = scrollView.contentOffset.y;
    NSLog(@"yOffset===%f",yOffset);
    CGFloat xOffset = (yOffset + kImageOriginHight)/2;
    if (yOffset < -kImageOriginHight) {
        CGRect f = self.topView.frame;
        f.origin.y = yOffset - kTempHeight;
        f.size.height =  -yOffset + kTempHeight;
        f.origin.x = xOffset;
        f.size.width = self.view.frame.size.width + fabsf(xOffset)*2;
        self.topView.frame = f;
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XDCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
//        cell.label1.text = self.name;
//        
//        cell.label2.text = self.recommended_reason;
//        
//        cell.abstractLabel.text = self.description_details;
//        
//        cell.addressLabel.text = self.address;
//        
//        NSLog(@"--------- text %@", self.recommended_reason);
//        
//        NSLog(@"--------- name%@", self.name);
    
    XDPlaceCityView *xdplaceCity = self.Arr[indexPath.row];
    
    cell.xdplaceCity = xdplaceCity;
    
    return cell;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createSubTableView];
    
    [self yangTuo];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
